package com.qbw.app.module.user.fragment;

import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.View;

import com.http.okhttp.HttpTask;
import com.lib.util.Util;
import com.qbw.app.Constant;
import com.qbw.app.R;
import com.qbw.app.databinding.UserFragmentRegistVerifycodeBinding;
import com.qbw.app.module.common.fragment.CommonFragment;
import com.qbw.app.module.common.util.HttpRequestUtil;
import com.qbw.app.module.common.util.UniversalToastUtil;
import com.qbw.app.module.common.view.NumberKeyboardView;
import com.qbw.app.module.user.activity.UserRegistActivity;
import com.qbw.app.request.impl.CommonRequest;
import com.qbw.core.observable.ObservableInt;
import com.qbw.core.observable.ObservableString;

/**
 * @author QBW
 * @date 2019/9/16
 */
public class UserRegistVerifyCodeFragment extends CommonFragment<UserFragmentRegistVerifycodeBinding> implements NumberKeyboardView.Listener {

    private Handler mHandler = new Handler();
    private ObservableInt mCountDown = new ObservableInt();
    private ObservableString mError = new ObservableString();

    public static UserRegistVerifyCodeFragment createInstance() {
        UserRegistVerifyCodeFragment ins = new UserRegistVerifyCodeFragment();
        return ins;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.user_fragment_regist_verifycode;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        UserRegistActivity activity = (UserRegistActivity) getActivity();
        mBinding.setCountryArea(activity.getCountryArea());
        mBinding.setPhone(activity.getPhone());
        mBinding.setError(mError);
        mBinding.setCountDownTime(mCountDown);
        mBinding.executePendingBindings();
        mBinding.keyboard.setListener(this);
        mCountDown.setValue(60);
        mHandler.postDelayed(mCountDownTimeRunn, 1000);
        mBinding.tvCountdownTime.setOnClickListener(this);
    }

    @Override
    public void onNumberKeyboardClickNumber(int number) {
        mBinding.vc.onInputNumber(number);
        if (mBinding.vc.isInputAllNumber()) {
            showLoadingDialog("");
            String verifyCode = mBinding.vc.getInputText();
            mGeneralManager.getRequestManager()
                           .getCommonRequest()
                           .checkVerifyCode(mBinding.getCountryArea().getValue(),
                                            mBinding.getPhone().getValue(),
                                            verifyCode,
                                            this)
                           .execute();

        }
    }

    @Override
    public void onNumberKeyboardClickBack() {
        mBinding.vc.onDel();
    }

    @Override
    public void onClick(View v) {
        int vid = v.getId();
        if (vid == R.id.tv_countdown_time) {
            mCountDown.setValue(60);
            mHandler.postDelayed(mCountDownTimeRunn, 1000);
            mGeneralManager.getRequestManager()
                           .getCommonRequest()
                           .getVerifyCode(mBinding.getCountryArea().getValue(),
                                          mBinding.getPhone().getValue(),
                                          Constant.VERIFYCODE_TYPE_REGIST,
                                          this)
                           .execute();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            Util.toogleKeybord(getActivity(), false);
        }
    }

    @Override
    public void onHttpSuccess(HttpTask httpTask, Object entity) {
        super.onHttpSuccess(httpTask, entity);
        String method = httpTask.getMethod();
        if (method.equals(CommonRequest.METHOD_CHECK_VERIFYCODE)) {
            mGeneralManager.getRequestManager()
                           .getCommonRequest()
                           .checkUserExist(mBinding.getCountryArea().getValue(),
                                           mBinding.getPhone().getValue(),
                                           this)
                           .execute();
        } else if (method.equals(CommonRequest.METHOD_USER_EXIST)) {
            closeLoadingDialog();
            afterVerify();
        }
    }

    @Override
    public void onHttpFailed(HttpTask httpTask, int errorCode, String message) {
        super.onHttpFailed(httpTask, errorCode, message);
        message = HttpRequestUtil.getMessage(errorCode, message);
        String method = httpTask.getMethod();
        if (method.equals(CommonRequest.METHOD_CHECK_VERIFYCODE)) {
            closeLoadingDialog();
            mError.setValue(message);
        } else if (method.equals(CommonRequest.METHOD_USER_EXIST)) {
            UniversalToastUtil.showFailedIconToast(message);
            closeLoadingDialog();
        }
    }

    private void afterVerify() {
        mError.setValue("");
        UserRegistActivity activity = (UserRegistActivity) getActivity();
        activity.getVerifyCode().setData(mBinding.vc.getInputText());
        activity.switchFragment(UserRegistActivity.FRAGMENT_AFTER_VERIFYCODE);
    }

    private Runnable mCountDownTimeRunn = new Runnable() {
        @Override
        public void run() {
            int n = mCountDown.getValue() - 1;
            if (n > 0) {
                mHandler.postDelayed(mCountDownTimeRunn, 1000);
            }
            mCountDown.setValue(n);
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mHandler.removeCallbacks(mCountDownTimeRunn);
    }
}
